package Builder;

import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;


public class LexicalAnalyzer {
	
	 private Symbles sym;
	 private StreamTokenizer in;

	    public LexicalAnalyzer(Reader r) {

	        in = new StreamTokenizer(r);

	        setupStreamToken();

	    }

	    private void setupStreamToken() {

	        in.resetSyntax();
	        in.eolIsSignificant(false);
	        in.wordChars('A', 'Z');
	        in.wordChars('a', 'z');
	        in.wordChars('0', '9');
	        in.wordChars('(', ')');
	        in.wordChars('[', ']');
	        in.wordChars('|', '|');
	        in.wordChars('<', '>');
	        in.wordChars('^', '^');
	        in.wordChars('!', '=');
	        in.wordChars('&', '&');

	        in.whitespaceChars('\u0000', ' ');
	    }

	    public String getString() {

	        return in.sval;
	    }// getString

	    public Symbles nextSymble() {
	        try {
	            switch (in.nextToken()) {

	                case StreamTokenizer.TT_EOF:
	                    sym = Symbles.EOF;
	                    break;

	                case StreamTokenizer.TT_WORD:
	                    // verifica prima se la parola e' riservata
	                    if (in.sval.equalsIgnoreCase("||")) {
	                        sym = Symbles.OR;
	                    } else if (in.sval.equalsIgnoreCase("&&")) {
	                        sym = Symbles.AND;
	                    } else if (in.sval.equalsIgnoreCase("!")) {
	                        sym = Symbles.NOT;
	                    } else if (in.sval.equalsIgnoreCase("relop")) {
	                        sym = Symbles.RELOP;
	                    } else if (in.sval.equalsIgnoreCase("==")) {
	                        sym = Symbles.EQ;
	                    } else if (in.sval.equalsIgnoreCase("!=")) {
	                        sym = Symbles.NEQ;
	                    } else if (in.sval.equalsIgnoreCase(">")) {
	                        sym = Symbles.GT;
	                    } else if (in.sval.equalsIgnoreCase(">=")) {
	                        sym = Symbles.GE;
	                    } else if (in.sval.equalsIgnoreCase("<")) {
	                        sym = Symbles.LT;
	                    } else if (in.sval.equalsIgnoreCase("<=")) {
	                        sym = Symbles.LE;
	                    } else if (in.sval.equalsIgnoreCase("^")) {
	                        sym = Symbles.POWER;
	                    } else if (in.sval.equalsIgnoreCase("/")) {
	                        sym = Symbles.DIV;
	                    } else if (in.sval.equalsIgnoreCase("%")) {
	                        sym = Symbles.REM;
	                    } else if (in.sval.equalsIgnoreCase("*")) {
	                        sym = Symbles.MULT;
	                    } else if (in.sval.equalsIgnoreCase("-")) {
	                        sym = Symbles.MINUS;
	                    } else if (in.sval.equalsIgnoreCase("+")) {
	                        sym = Symbles.PLUS;
	                    } else if (in.sval.equalsIgnoreCase("(")) {
	                        sym = Symbles.TONDA_APERTA;
	                    } else if (in.sval.equalsIgnoreCase(")")) {
	                        sym = Symbles.TONDA_CHIUSA;
	                    } else if (in.sval.equalsIgnoreCase("[")) {
	                        sym = Symbles.QUADRA_APERTA;
	                    } else if (in.sval.equalsIgnoreCase("]")) {
	                        sym = Symbles.QUADRA_CHIUSA;
	                    } else {// identifica la parola non riservata

	                        try {

	                            Integer.parseInt(this.getString());
	                            sym = Symbles.NUM;
	                        } catch (NumberFormatException e) {

	                            sym = Symbles.ID;
	                        }


	                    }
	                    break;

	                default:
	                    sym = Symbles.CHAR_INVALIDO;
	                    break;
	            }
	        } catch (IOException e) {

	            sym = Symbles.EOF;
	        }
	        return sym;
	    }// prossimoSimbolo


}
